"""
Case Type   : 系统视图PG_PREPARED_STATEMENT
Case Name   : global plancache开启的时候，在多个会话创建prepare语句，调用函数查看数量是否正确
Create At   : 2023/12/12
Owner       : ningyali
Description :
    1.开启线程池,打开参数enable_global_plancache
    2.建表
    3.新建1000个session，分别创建预备语句
    4.查询global_plancache_status视图
    5.传入0查询视图
    6.清理环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.可查询到所有的预备语句
    5.可查询到所有的预备语句
    6.执行成功
History     :
"""

import os
import subprocess
import unittest

from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class PgPreparedStatement38(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.t_name = 't_prepare_38'
        self.java_fname = 'pg_prepared_statement_case0038.java'
        self.jar_name, self.jar_path = self.common.get_jdbc('opengauss')
        self.conn_num = 1000

    def test_1(self):
        text = 'step1:开启线程池,打开参数enable_global_plancache; expect:执行成功'
        self.log.info(f'-----{text}-----')
        guc_res = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'enable_thread_pool=on')
        self.assertTrue(guc_res, f'执行失败：{text}')
        guc_res = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'enable_global_plancache=on')
        self.assertTrue(guc_res, f'执行失败：{text}')
        restart_res = self.pri_sh.restart_db_cluster()
        self.assertTrue(restart_res, f'执行失败：{text}')

        text = 'step2:调用java脚本执行第2~5步; expect:执行成功'
        self.log.info(f'-----{text}-----')
        self.log.info(f'-----拷贝java脚本到可执行路径-----')
        java_fpath = os.path.join(macro.SCRIPTS_PATH, 'SystemView',
                                  self.java_fname)
        cmd = f'cp {java_fpath} {os.path.dirname(self.jar_path)}'
        self.log.info(cmd)
        res = subprocess.getoutput(cmd)
        self.log.info(res)
        self.assertEqual(len(res.strip()), 0, f'执行失败：{text}')

        self.log.info(f'-----编译java脚本-----')
        cmd = f'cd {os.path.dirname(self.jar_path)};' \
            f'javac -encoding utf-8 -cp {self.jar_name} {self.java_fname}'
        self.log.info(cmd)
        res = subprocess.getoutput(cmd)
        self.log.info(res)
        self.assertEqual(len(res.strip()), 0, f'执行失败：{text}')

        self.log.info(f'-----执行编译后脚本-----')
        cmd = f'cd {os.path.dirname(self.jar_path)};' \
            f'java -cp {self.jar_name}:./ {self.java_fname.split(".")[0]} ' \
            f'{self.pri_sh.node.db_host} {self.pri_sh.node.db_port} ' \
            f'{self.pri_sh.node.db_name} {self.pri_sh.node.db_user} ' \
            f'{self.pri_sh.node.db_password} {self.conn_num}'
        self.log.info(cmd)
        res = subprocess.getoutput(cmd)
        self.log.info(res)
        self.assertIn(f'count\n{self.conn_num}\n', res, f'执行失败：{text}')
        self.assertNotIn('error', res.lower(), f'执行失败：{text}')

    def tearDown(self):
        self.log.info('----run teardown----')
        text1 = 'step6:关闭线程池; expect:执行成功'
        self.log.info(f'-----{text1}-----')
        guc_res = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'enable_thread_pool=off')
        guc_res1 = self.pri_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             'enable_global_plancache=off')
        restart_res = self.pri_sh.restart_db_cluster()

        text2 = 'step7:删除java脚本及编译后脚本; expect:执行成功'
        self.log.info(f'-----{text2}-----')
        f_path = os.path.join(os.path.dirname(self.jar_path),
                              self.java_fname.split(".")[0]+"*")
        rm_cmd = f'rm -rf {f_path}'
        self.log.info(rm_cmd)
        rm_res = subprocess.getoutput(rm_cmd)
        self.log.info(rm_res)

        self.assertTrue(guc_res, f'执行失败：{text1}')
        self.assertTrue(guc_res1, f'执行失败：{text1}')
        self.assertTrue(restart_res, f'执行失败：{text1}')
        self.assertEqual(len(rm_res.strip()), 0, f'执行失败：{text2}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
